import request from '@/utils/request'

/**
 * 获取活动列表
 * @param {Object} params - 查询参数 {pageNum, pageSize, keyword, status, creatorId}
 * @returns {Promise}
 */
export function getEventList(params) {
  return request({
    url: '/api/events',
    method: 'get',
    params
  })
}

/**
 * 获取活动详情
 * @param {Number} id - 活动ID
 * @returns {Promise}
 */
export function getEventDetail(id) {
  return request({
    url: `/api/events/${id}`,
    method: 'get'
  })
}

/**
 * 参与活动
 * @param {Number} id - 活动ID
 * @returns {Promise}
 */
export function joinEvent(id) {
  return request({
    url: `/api/events/${id}/join`,
    method: 'post'
  })
}

/**
 * 取消参与活动
 * @param {Number} id - 活动ID
 * @returns {Promise}
 */
export function cancelJoinEvent(id) {
  return request({
    url: `/api/events/${id}/cancel`,
    method: 'post'
  })
}

/**
 * 创建新活动
 * @param {Object} data - 活动数据，包含title, cover, description, location, startTime, endTime, status等
 * @returns {Promise} 接口返回格式：{ code: 200, msg: "操作成功", data: { id: 1, title: "活动标题", ... } }
 * 
 * 请求参数示例:
 * {
 *   "title": "活动标题",
 *   "cover": "活动封面图片URL",
 *   "description": "活动描述内容",
 *   "location": "活动地点",
 *   "startTime": "2023-05-20 09:00:00",
 *   "endTime": "2023-05-20 18:00:00",
 *   "status": "draft/online/offline",
 *   "notes": "活动须知（可选）"
 * }
 * 
 * 返回数据示例:
 * {
 *   "code": 200,
 *   "msg": "创建成功",
 *   "data": {
 *     "id": 1,
 *     "title": "活动标题",
 *     "cover": "http://example.com/images/cover.jpg",
 *     "description": "活动描述内容",
 *     "location": "活动地点",
 *     "startTime": "2023-05-20 09:00:00",
 *     "endTime": "2023-05-20 18:00:00",
 *     "status": "draft",
 *     "createTime": "2023-05-15 10:30:00",
 *     "creatorId": 1,
 *     "creatorName": "管理员",
 *     "notes": "活动须知",
 *     "participantCount": 0
 *   }
 * }
 */
export function createEvent(data) {
  return request({
    url: '/api/events',
    method: 'post',
    data
  })
}

/**
 * 更新活动
 * @param {Number} id - 活动ID
 * @param {Object} data - 活动数据
 * @returns {Promise}
 */
export function updateEvent(id, data) {
  return request({
    url: `/api/events/${id}`,
    method: 'put',
    data
  })
}

/**
 * 删除活动
 * @param {Number} id - 活动ID
 * @returns {Promise} 接口返回格式：{ code: 200, msg: "删除成功", data: null }
 */
export function deleteEvent(id) {
  return request({
    url: `/api/events/${id}`,
    method: 'delete'
  })
}

/**
 * 上传活动封面图片
 * @param {File} file - 图片文件
 * @returns {Promise} 接口返回格式：{ code: 200, msg: "上传成功", data: "/images/20250401_123456.jpg" }
 */
export function uploadEventCover(file) {
  const formData = new FormData()
  formData.append('file', file)
  
  return request({
    url: '/api/files/upload/cover',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
} 